//
//  ETUpLoadImage.m
//  etaoLocal
//
//  Created by 稳 张 on 12-5-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UpLoadImageModel.h"
//#import "ETUrlEngine.h"
#import "ASIFormDataRequest.h"


@interface UpLoadImageModel()<ASIHTTPRequestDelegate,ASIProgressDelegate>

@property (strong) ASIFormDataRequest* request;
@property (strong) NSData* postData;

@end

@implementation UpLoadImageModel

@synthesize SuccessReturnStr = _SuccessReturnStr;
@synthesize FailReturnStr = _FailReturnStr;
//@synthesize progress = _progress;
@synthesize request = _request;
@synthesize postData = _postData;


@synthesize delegate;


-(void) upLoadImages:(UIImage*) image {
    
    NSMutableDictionary* tempDic = [[NSMutableDictionary alloc] init];
    [tempDic setObject:@"1.0" forKey:@"v"];
    
//    ETUrlEngine* tempUrlEngine = [[ETUrlEngine alloc] init];
//    [tempUrlEngine setNeedUserSession: YES];
//    NSDictionary* tempApi3Dic = [tempUrlEngine getPostDicByAPI3SignByTypeWithPartmentAndData:tempDic date:nil];
//    
//    NSURL *nsUrl = [[NSURL alloc] initWithString:[tempUrlEngine httpHeadUrl]];
//    self.request = [[ASIFormDataRequest alloc] initWithURL:nsUrl];
    
    [self.request setDelegate:self];
    [self.request setUploadProgressDelegate:self];
    
    //别忘记showAccurateProgress也要设置为YES (默认为NO,则只显示0%和100%):
    self.request.showAccurateProgress=YES;//
    
    //UIImage* image = [[myShopPhontos objectAtIndex:0] image];
    self.postData = UIImageJPEGRepresentation(image, 0.80);
    
    
//    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[[tempApi3Dic allKeys] count]];
//    
//    for (NSString *key in [tempApi3Dic keyEnumerator]) {
//        NSObject *val = [tempApi3Dic objectForKey:key];
//        
//        if ([val isKindOfClass:[NSData class]]) {
//            self.request.postFormat = ASIMultipartFormDataPostFormat;
//            [self.request addData:self.postData withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:key];
//        } else {
//            [self.request addPostValue:val forKey:key];
//            [arr addObject:[NSString stringWithFormat:@"%@=%@", key, val]];
//        }
//        
//    }
    
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
//    
//    [self.request startAsynchronous]; //异步执行
}


- (void)requestFinished:(ASIHTTPRequest *)request {
    
    NSData *data = [NSData dataWithData:[request responseData]]; 	
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingUTF8);
    NSString* temPStr = [[NSString alloc] initWithData:data encoding:enc]; 
    
    self.SuccessReturnStr = temPStr;
        
    if (delegate && [delegate respondsToSelector:@selector(postDataFinished:)]) {
        [delegate performSelector:@selector(postDataFinished:)withObject:self];
    }

}


- (void)requestFailed:(ASIHTTPRequest *)request {
    
    NSData *data = [NSData dataWithData:[request responseData]]; 	
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingUTF8);
    NSString* temPStr = [[NSString alloc] initWithData:data encoding:enc]; 
    
    self.FailReturnStr = temPStr;
    
    if (delegate && [delegate respondsToSelector:@selector(postDataFailed:)]) {
        [delegate performSelector:@selector(postDataFailed:)withObject:self];
    }
}


//- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes {
//    
//}


- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength {
    NSLog(@"totalupload:%lld",newLength);
    
//    NSNumber* tempNum = [[NSNumber alloc] initWithDouble:(newLength/[request postLength])];
//    
//    self.progress = tempNum;
//    
//    if (delegate && [delegate respondsToSelector:@selector(postDataProgress:)]) {
//        [delegate performSelector:@selector(postDataProgress:)withObject:self];
//    }
}



@end
